package com.itheima.utils;

import com.itheima.pojo.CheckGroup;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class POIDownloadUtils {
    public static void download(String tablename,HttpServletResponse response, List<CheckGroup> checkGroupList, String fileType,List<String> columnNames) throws IOException, IllegalAccessException {
        //调用方法获取工作簿对象
        Workbook workbook=getWordBook(fileType);
        //设置sheet页名称
        Sheet sheet = workbook.createSheet(tablename);
        //创建表头行
        Row headerRow = sheet.createRow(0);
        //设置表头内容,由静态改成动态
        for (int i = 0; i < columnNames.size(); i++) {
            headerRow.createCell(i).setCellValue(columnNames.get(i));
        }
//        headerRow.createCell(0).setCellValue("检查组id");
//        headerRow.createCell(1).setCellValue("检查组编码");
//        headerRow.createCell(2).setCellValue("检查组名称");
//        headerRow.createCell(3).setCellValue("检查组助记码");
//        headerRow.createCell(4).setCellValue("适用性别");
//        headerRow.createCell(5).setCellValue("介绍");
//        headerRow.createCell(6).setCellValue("注意事项");
        //遍历结果集，读取行数据
        for (int i = 0; i < checkGroupList.size(); i++) {
            //创建行，从第二行开始
            Row row = sheet.createRow(i+1);
            //获取行对象
            CheckGroup checkGroup = checkGroupList.get(i);

            //反射，将对象数据依次填充到单元格中
            Field[] declaredFields = checkGroup.getClass().getDeclaredFields();
            for (int j = 0; j < declaredFields.length-1; j++) {//最后一个字段不遍历
                declaredFields[j].setAccessible(true);
                Object o = declaredFields[j].get(checkGroup);
                if(o==null){//防止空指针异常
                    o="";
                }
                row.createCell(j).setCellValue(o.toString());
            }

//            row.createCell(0).setCellValue(checkGroup.getId());
//            row.createCell(1).setCellValue(checkGroup.getCode());
//            row.createCell(2).setCellValue(checkGroup.getName());
//            row.createCell(3).setCellValue(checkGroup.getHelpCode());
//            row.createCell(4).setCellValue(checkGroup.getSex());
//            row.createCell(5).setCellValue(checkGroup.getRemark());
//            row.createCell(6).setCellValue(checkGroup.getAttention());
        }
        //设置返回数据
        response.setContentType("application/msexcel;charset=UTF-8");
        SimpleDateFormat newsdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String date = newsdf.format(new Date());

        response.addHeader("Content-Disposition", "attachment;filename=\""
                + new String((tablename +"_"+ date + "."+fileType).getBytes("GBK"),
                "ISO8859_1") + "\"");

        //获取输出流
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    private static Workbook getWordBook(String fileType) {
        if(fileType!=null && fileType.length()>0){//判断前台输入的文件类型有值
            if(fileType.equals("xls")){
                return new HSSFWorkbook();
            }else {
                return new XSSFWorkbook();
            }
        }else{
            return null;
        }
    }
}
